<?php
//
//	file: includes/calendar/topics/class_calendar_topics_parse.php
//	author: ptirhiik
//	begin: 04/05/2006
//	version: 1.6.1 - 09/02/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

if ( !class_exists('message') )
{
	include($config->url('includes/class_message'));
}

class calendar_message extends message
{
	function parse($row, $max_chars=0, $javascript=true)
	{
		$message = $row['post_text'];
		if ( $javascript )
		{
			$switches = array(
				'html' => $row['enable_html'],
				'bbcode' => $row['enable_bbcode'],
				'smilies' => $row['enable_smilies'],
				'magic_url' => isset($row['magic_url']) ? $row['magic_url'] : true,
				'censor' => isset($row['censor']) ? $row['censor'] : true,
			);
			parent::parse($message, $row['bbcode_uid'], $switches, '', $max_chars);
		}
		else
		{
			$message = _clean_bbcode(_clean_html($message));
			if ( $max_chars && (strlen($message) > $max_chars + 3) )
			{
				$message = substr($row['post_text'], 0, $max_chars) . '...';
			}
			$message = _censor($message);

			// change \n into <br />
			$message = preg_replace("/[\n\r]{1,2}/", '<br />', $message);
		}
		return $message;
	}
}

class calendar_forums_select
{
	function calendar_forums_select()
	{
		global $config, $user, $forums;

		// read forums and auths relative to if not done
		if ( $forums === false )
		{
			if ( !class_exists('forums') )
			{
				include($config->url('includes/class_forums'));
			}
			$forums = new forums();
			$forums->read();
		}
		if ( !isset($user->cache[POST_FORUM_URL]) )
		{
			$user->get_cache(POST_FORUM_URL);
		}
	}

	function retrieve_ids($forum_id=0)
	{
		global $user, $forums;

		$forum_id = intval($forum_id);
		if ( !isset($forums->data[$forum_id]) )
		{
			return false;
		}

		$tkeys = array_flip($forums->keys);
		$from = $tkeys[$forum_id];
		$to = $tkeys[ $forums->data[$forum_id]['last_child_id'] ];
		unset($tkeys);
		$forum_ids = array();
		for ( $i = $from; $i <= $to; $i++ )
		{
			if ( ($forums->data[ $forums->keys[$i] ]['forum_type'] == POST_FORUM_URL) && $user->auth(POST_FORUM_URL, 'auth_read', $forums->keys[$i]) )
			{
				$forum_ids[] = $forums->keys[$i];
			}
		}
		return $forum_ids;
	}

	function display_nav($row, $tpl_switch)
	{
		global $template, $config, $user, $forums;

		$locs = array();
		$cur = intval($row['forum_id']);
		while ( intval($cur) )
		{
			if ( !isset($forums->data[$cur]) )
			{
				break;
			}
			if ( $user->auth(POST_FORUM_URL, 'auth_view', $cur) )
			{
				$locs[] = array(
					'txt' => $user->lang($forums->data[$cur]['forum_name']),
					'url' => INDEX,
					'parms' => array(POST_FORUM_URL => $cur),
				);
			}
			$cur = $forums->data[$cur]['forum_main'];
		}

		for ( $i = count($locs) - 1; $i >= 0; $i-- )
		{
			$template->assign_block_vars($tpl_switch, array(
				'U_NAV' => $config->url($locs[$i]['url'], $locs[$i]['parms'], true),
				'NAV' => $locs[$i]['txt'],
			));
			$template->set_switch($tpl_switch . '.sep', $i > 0);
		}
	}

	function build_select($varname, &$options, &$value)
	{
		global $forums, $user;

		$original_value = intval($value);
		$value = 0;

		// get option list
		$options = array();
		if ( !($front_pic = $forums->get_front_pic()) )
		{
			return false;
		}
		foreach ( $front_pic as $cur_id => $front )
		{
			$option = '';
			$count_front = strlen($front);
			for ( $i = 0; $i < $count_front; $i++ )
			{
				$option .= $user->lang('tree_pic_' . $front[$i]);
			}
			if ( isset($forums->data[$cur_id]) )
			{
				$option .= !$cur_id ? $user->lang($forums->data[$cur_id]['forum_name']) : $forums->data[$cur_id]['forum_name'];
			}
			$options[$cur_id] = $option;
		}

		// current value
		$value = _read($varname, TYPE_INT, $original_value, $options);
		if ( !isset($forums->data[$value]) )
		{
			$value = 0;
		}
		return true;
	}
}

?>